package jp.sourceforge.akjrcp.text_douga.effect;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;

import jp.sourceforge.akjrcp.text_douga.DougaPreference;

public class FillColorEffect extends VideoEffect{
	protected int fillColor;
	
	
	public String toString(){
		String out="FillColorEffect:color="+fillColor+" "+super.toString();
			return out;
	}
	
	public FillColorEffect(int color){
		this.fillColor=color;
	}
	
	public float getFrameTransparent(double t,double maxtime) {
		
		double time=Math.abs(t);
		
		if(time<fullEffectTime){
			return 1;//draw full
		}
		//should reverse?
		float value=1-Math.abs((float)((time-fullEffectTime)/(maxtime-fullEffectTime)));
		if(value>1){
			log.warn("invalid: t="+t+" max="+maxtime+" v="+value);
			return 1;
		}else{
			log.info("t="+t+" max="+maxtime+" v="+value);
		}
		return value;
	}
	
	@Override
	public void effectAfter(DougaPreference preference, BufferedImage baseImage, int frame,double time) throws IOException {
		Graphics2D g2=(Graphics2D) baseImage.getGraphics();
		g2.setColor(new Color(fillColor));
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getFrameTransparent(time, getAfterTime())));
		g2.fillRect(0, 0, baseImage.getWidth(), baseImage.getHeight());
	}

	@Override
	public void effectAll(DougaPreference preference, BufferedImage baseImage, int frame) throws IOException {
		Graphics2D g2=(Graphics2D) baseImage.getGraphics();
		g2.setColor(new Color(fillColor));
		g2.fillRect(0, 0, baseImage.getWidth(), baseImage.getHeight());
	}
	


	@Override
	public void effectBefore(DougaPreference preference, BufferedImage baseImage, int frame,double time) throws IOException {
		Graphics2D g2=(Graphics2D) baseImage.getGraphics();

		g2.setColor(new Color(fillColor));
		float f=getFrameTransparent(time, getBeforeTime());
		
		log.info("trans="+f);
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, f));
		
		g2.fillRect(0, 0, baseImage.getWidth(), baseImage.getHeight());
	}




	public int getFillColor() {
		return fillColor;
	}

	public void setFillColor(int fillColor) {
		this.fillColor = fillColor;
	}

	@Override
	public void effectKeyFrame(DougaPreference preference, BufferedImage baseImage, int frame, double time) throws IOException {
		Graphics2D g2=(Graphics2D) baseImage.getGraphics();
		g2.setColor(new Color(fillColor));
		g2.fillRect(0, 0, baseImage.getWidth(), baseImage.getHeight());
	}

}
